package com.icinfo.cloud.provider.punish.strategy.appcase;

import com.icinfo.cloud.provider.common.client.IUserCenterClient;
import com.icinfo.cloud.provider.common.enums.AssociatedTaskTypeEnum;
import com.icinfo.cloud.provider.common.enums.DocumentKindEnum;
import com.icinfo.cloud.provider.common.enums.PartiesTypeEnum;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.punish.dto.CheckObjectDeleteZlgztzs;
import com.icinfo.cloud.provider.punish.dto.CheckObjectInfoBySourceIdDto;
import com.icinfo.cloud.provider.punish.dto.CheckObjectSaveZlgztzsDto;
import com.icinfo.cloud.provider.punish.dto.CheckObjectZtcSourceTypeDto;
import com.icinfo.cloud.provider.punish.enums.ZtcSourceTypeEnum;
import com.icinfo.cloud.provider.punish.service.IAppDocumentInfoDetailService;
import com.icinfo.cloud.provider.punish.vo.AssociatedTaskPartiesVo;
import com.icinfo.cloud.provider.punish.vo.AssociatedTaskVo;
import com.icinfo.cloud.provider.punish.vo.ClientVO;
import com.icinfo.cloud.provider.punish.vo.TaskClientVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;


@Component
public class CheckObjectZtcSourceTypeHook extends BaseZtcSourceTypeHook {
    @Resource
    private IUserCenterClient userCenterClient;

    @Resource
    private IAppDocumentInfoDetailService appDocumentInfoDetailService;

    @Override
    public ZtcSourceTypeEnum getSourceType() {
        return ZtcSourceTypeEnum.CHECK_OBJECT;
    }

    @Override
    public void appCaseSaveHook(CheckObjectZtcSourceTypeDto dto) {
        if (!DocumentKindEnum.DOC_KIND_ZLGZTZS.equals(dto.getDocumentKindEnum())) {
            return;
        }
        // 获取关联任务相关信息，插入detail的associatedInfo字段
        updateAssociatedTask(dto);
        // 回调检查接口
        CheckObjectSaveZlgztzsDto zlgztzsDto = new CheckObjectSaveZlgztzsDto();
        zlgztzsDto.setClientId(dto.getSourceId());
        zlgztzsDto.setDocId(dto.getDocumentId());
        zlgztzsDto.setTaskOrgId(dto.getTaskOrgId());
        userCenterClient.checkObjectSaveZlgztzs(zlgztzsDto);
    }

    /**
     * 更新关联任务信息
     */
    private void updateAssociatedTask(CheckObjectZtcSourceTypeDto dto) {
        // 查询关联任务信息
        CheckObjectInfoBySourceIdDto query = new CheckObjectInfoBySourceIdDto();
        query.setSourceId(dto.getSourceId());
        query.setTaskOrgId(dto.getTaskOrgId());
        TaskClientVO taskClientVO = userCenterClient.getCheckObjectInfoBySourceId(query);
        // 组装关联任务信息
        AssociatedTaskVo associatedTaskVo = assembleAssociatedTaskVo(taskClientVO);
        if (1 != appDocumentInfoDetailService.updateAssociatedTaskById(dto.getDocumentId(), Collections.singletonList(associatedTaskVo))) {
            throw new BusinessException("文书详情信息表关联任务更新失败！");
        }
    }

    /**
     * 组装关联任务信息
     */
    private static AssociatedTaskVo assembleAssociatedTaskVo(TaskClientVO taskClientVO) {
        AssociatedTaskVo associatedTaskVo = new AssociatedTaskVo();
        associatedTaskVo.setId(taskClientVO.getTaskUid());
        associatedTaskVo.setName(taskClientVO.getTaskName());
        associatedTaskVo.initRelationTime();
        associatedTaskVo.setType(AssociatedTaskTypeEnum.CHECK_TASK);
        List<ClientVO> clients = taskClientVO.getClients();
        ClientVO clientVO = clients.stream().findFirst().orElse(null);
        if (Objects.isNull(clientVO)) {
            throw new BusinessException("关联任务无当事人信息");
        }
        AssociatedTaskPartiesVo partiesVo = AssociatedTaskPartiesVo.convertForCheckTask(clientVO);
        if (Objects.isNull(partiesVo)) {
            throw new BusinessException("关联任务当事人信息转换失败");
        }
        associatedTaskVo.setCheckObject(PartiesTypeEnum.PERSON.equals(partiesVo.getType()) ? partiesVo.getPersonName() : partiesVo.getEntName());
        return associatedTaskVo;
    }

    @Override
    public void appCaseDeleteHook(CheckObjectZtcSourceTypeDto dto) {
        if (!DocumentKindEnum.DOC_KIND_ZLGZTZS.equals(dto.getDocumentKindEnum())) {
            return;
        }
        //组装参数
        CheckObjectDeleteZlgztzs zlgztzsDto = new CheckObjectDeleteZlgztzs();
        zlgztzsDto.setDocId(dto.getDocumentId());
        userCenterClient.checkObjectDeleteZlgztzs(zlgztzsDto);
    }


}
